package com.example.sun.myapplication.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.example.sun.myapplication.GetLightDeviceStateBean;
import com.example.sun.myapplication.InnovationClient;
import com.example.sun.myapplication.NetWorkCallBack;
import com.example.sun.myapplication.R;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.json.JSONException;
import org.json.JSONObject;

import cz.msebera.android.httpclient.Header;

public class MainActivity extends Activity implements View.OnClickListener {

    public static final String TAG = MainActivity.class.getSimpleName();

    // 页面初始化时调用，如果页面没销毁，再次显示该页面时不会再次调用此方法
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.w(TAG, "onCreate()");

        if (savedInstanceState != null) {
            String userName = savedInstanceState.getString("userName");
        }
        // 加载布局
        setContentView(R.layout.activity_main);
        // 获取相应的控件
        TextView tvLoginBtn = findViewById(R.id.tv_login_btn);
        TextView tvRegBtn = findViewById(R.id.tv_reg_btn);

        // 设置点击事件
        tvLoginBtn.setOnClickListener(this);
        tvRegBtn.setOnClickListener(this);
    }



    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("userName", "zhangsan");

    }

    // 页面显示（处于栈顶）时会调用此方法
    @Override
    protected void onResume() {
        super.onResume();
        Log.w(TAG, "onResume()");

//        finish();
    }

    // 页面即将隐藏时（处于非栈顶）时会调用此方法
    @Override
    protected void onPause() {
        Log.w(TAG, "onPause()");
        super.onPause();

    }

    // 页面即将销毁时调用此方法
    @Override
    protected void onDestroy() {
        Log.w(TAG, "onDestroy()");
        super.onDestroy();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_login_btn:
                GetLightDeviceStateBean bean = new GetLightDeviceStateBean();

                Intent intent = new Intent(this, LoginActivity.class);
                intent.putExtra("userName", "zhangsan");
                intent.putExtra("bean", bean);
                startActivity(intent);
                break;
            case R.id.tv_reg_btn:

                break;
        }
    }


    private void getLightDeviceState(String eqCode, final NetWorkCallBack callBack) {

        String url = "http://101.200.58.68:8181/api/6/Pull_LightState?App=Care";
        Log.e("/Pull_LightState", url);

        GetLightDeviceStateBean infoBean = new GetLightDeviceStateBean();
        infoBean.setUN("111111");
        infoBean.setAccessToken("123456");
        infoBean.setSC("1eb3253f3acb44c0a18b0bfbb0dac3d7");
        infoBean.setSV("62dc047cb4fd43b3a59c3714a7bc368f");
        infoBean.setCode(eqCode);

        Gson gson = new Gson();
        String re = gson.toJson(infoBean);

        RequestParams request = new RequestParams();
        request.add("content", re);
        Log.e("/Pull_LightState", re);

        InnovationClient.getInstance().post(this, url, request, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                Log.e("/Pull_LightState", new String(responseBody));
                String result = new String(responseBody);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    if (jsonObject.getString("ResultMessage").equals("1000")) {
                        JSONObject resultJson = new JSONObject(jsonObject.getString("ReturnValue"));

                        if (callBack != null) {
                            callBack.onSuccess(resultJson.toString());
                            return;
                        }

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                if (callBack != null) {
                    callBack.onError(result);
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                Log.e("Pull_LightState-result", "请求失败");
            }
        });

    }

}
